/* Author: Nahuel F. Lema

 */
var faqController = {
    //CATEGORY LIST
    caetegoryList: {
        defs: {
            loaded: false,
            printed: false,
            container: $('.category-list'),
            content: [],
            tpl: $('#categoryListTpl').html()
        },
        bind: function () {
            $(this.defs.container).find('a').click(function (event) {
                event.preventDefault();
                faqController.urlParser.encode($(this).data('uri'));
                faqController.urlParser.decode(faqController.contentManager.switchView);
                $('#faq-list-title').html($(this).html())
            })
        },
        get: function(){
            var self = this;
            $.ajax({
                url: scriptsController.defs.baseUrl+'controllers/faqController/categorias',
                success: function (data) {
                    if (data){
                        self.defs.content = data;
                        self.defs.loaded = true;
                        self.print();
                        self.bind();
                    }
                }
            })
        },
        print: function () {
            var html = '';
            for (var n in this.defs.content) {
                html += this.defs.tpl
                    .replace(/\$\{id\}/g, this.defs.content[n].id)
                    .replace(/\$\{uri\}/g,  this.defs.content[n].uri)
                    .replace(/\$\{contenido\}/g,  this.defs.content[n].contenido)
                    .replace(/\$\{baseUrl\}/g,  scriptsController.defs.baseUrl)
            }
            $(this.defs.container).html(html);
            this.defs.printed = true;
        },
        show: function () {
            if (this.defs.printed) {
                $(this.defs.container).show();
            } else if (this.defs.load) {
                this.print();
            } else {
                this.get();
            }
        }
    },
    //PREGUNTA BY ID
    preguntaById: {
         defs: {
            container: $('#preguntaById'),
            content: [],
            tpl: $('#preguntaByIdTpl').html()
        },
        get: function(i){
            var self = this;
            $.ajax({
                url: scriptsController.defs.baseUrl+'controllers/faqController/preguntaById/'+i,
                success: function (data) {
                    if (data){
                        self.defs.content = data;
                        self.defs.loaded = true;
                        self.print();
                    }
                    faqController.tallyCounter.addVisitante(i)
                }
            })
        },
        print: function () {
            var html = '';
            html += this.defs.tpl
                .replace(/\$\{id\}/g, this.defs.content.id)
                .replace(/\$\{uri\}/g,  this.defs.content.uri)
                .replace(/\$\{pregunta\}/g,  this.defs.content.pregunta)
                .replace(/\$\{respuesta\}/g,  this.defs.content.respuesta)
                .replace(/\$\{baseUrl\}/g,  scriptsController.defs.baseUrl)
            $(this.defs.container).html(html).show();
        },
        show: function (i) {
            this.get(i);
        },
        hide: function () {
            $(this.defs.container).hide();
        }
    },
    //TOP TEN
    topTen: {
         defs: {
            loaded: false,
            printed: false,
            container: $('#topTen'),
            content: [],
            tpl: $('#faqListTpl').html()
        },
        bind: function () {
            $(this.defs.container).find('a').click(function (event) {
                var self = this;
                event.preventDefault();
                $.scrollTo(
                    $('#faq-page'),
                    800,
                    {onAfter:function (){
                        console.log(99)
                        faqController.urlParser.encode($(self).data('uri'));
                        faqController.urlParser.decode(faqController.contentManager.switchView);
                    }
                })
            })
        },
        get: function(){
            var self = this;
            $.ajax({
                url: scriptsController.defs.baseUrl+'controllers/faqController/topTen',
                success: function (data) {
                    if (data){
                        self.defs.content = data;
                        self.defs.loaded = true;
                        self.print();
                        self.bind();
                    }
                }
            })
        },
        print: function () {
            var html = '';
            for (var i=0, l=this.defs.content.length; i<l; i++) {
                html += this.defs.tpl
                    .replace(/\$\{i\}/g, i+1)
                    .replace(/\$\{id\}/g, this.defs.content[i].id)
                    .replace(/\$\{uri\}/g,  this.defs.content[i].uri)
                    .replace(/\$\{pregunta\}/g,  this.defs.content[i].pregunta)
                    .replace(/\$\{baseUrl\}/g,  scriptsController.defs.baseUrl)
            }
            $(this.defs.container).html(html).show();
            $('#faq-list-title').html('Top Ten')
            this.defs.printed = true;
        },
        show: function () {
            if (this.defs.printed) {
                $(this.defs.container).show();
            } else if (this.defs.load) {
                this.print();
            } else {
                this.get();
            }
        },
        hide: function () {
            $(this.defs.container).hide();
        }
    },
    //PREGUNTAS BY CATEGORIAS
    preguntasByCategorias: {
         defs: {
            container: $('#preguntasByCategorias'),
            content: [],
            tpl: $('#faqListTpl').html()
        },
        bind: function () {
            $(this.defs.container).find('a').click(function (event) {
                var self = this;
                event.preventDefault();
                $.scrollTo(
                    $('#faq-page'),
                    800,
                    {onAfter:function (){
                        console.log(99)
                        faqController.urlParser.encode($(self).data('uri'));
                        faqController.urlParser.decode(faqController.contentManager.switchView);
                    }
                })
            })
        },
        get: function(i){
            var self = this;
            $.ajax({
                url: scriptsController.defs.baseUrl+'controllers/faqController/selectByCategoria/'+i,
                success: function (data) {
                    if (data){
                        self.defs.content = data;
                        self.print();
                        self.bind();
                    }
                }
            })
        },
        print: function () {
            var html = '';
            for (var i=0, l=this.defs.content.length; i<l; i++) {
                html += this.defs.tpl
                    .replace(/\$\{i\}/g, i+1)
                    .replace(/\$\{id\}/g, this.defs.content[i].id)
                    .replace(/\$\{uri\}/g,  this.defs.content[i].uri)
                    .replace(/\$\{pregunta\}/g,  this.defs.content[i].pregunta)
                    .replace(/\$\{baseUrl\}/g,  scriptsController.defs.baseUrl)
            }
            $(this.defs.container).html(html).show()
        },
        show: function (i) {
            this.get(i);
        },
        hide: function () {
            $(this.defs.container).hide();
        }
    },
    //PREGUNTAS RELACIONADAS
    preguntasRelacionadas: {
         defs: {
            container: $('#preguntasRelacionadas'),
            content: [],
            tpl: $('#faqListTpl').html()
        },
        bind: function () {
            $(this.defs.container).find('a').click(function (event) {
                var self = this;
                event.preventDefault();
                $.scrollTo(
                    $('#faq-page'),
                    800,
                    {onAfter:function (){
                        console.log(99)
                        faqController.urlParser.encode($(self).data('uri'));
                        faqController.urlParser.decode(faqController.contentManager.switchView);
                    }
                })
            })
        },
        get: function(i){
            var self = this;
            $.ajax({
                url: scriptsController.defs.baseUrl+'controllers/faqController/preguntasRelacionadas/'+i,
                success: function (data) {
                    if (data){
                        self.defs.content = data;
                        self.print();
                        self.bind();
                    }
                }
            })
        },
        print: function () {
            var html = '';
            for (var i=0, l=this.defs.content.length; i<l; i++) {
                html += this.defs.tpl
                    .replace(/\$\{i\}/g, i+1)
                    .replace(/\$\{id\}/g, this.defs.content[i].id)
                    .replace(/\$\{uri\}/g,  this.defs.content[i].uri)
                    .replace(/\$\{pregunta\}/g,  this.defs.content[i].pregunta)
                    .replace(/\$\{baseUrl\}/g,  scriptsController.defs.baseUrl)
            }
            $(this.defs.container).html(html).show();
            $('#faq-list-title').html('Preguntas Relacionadas')
        },
        show: function (i) {
            this.get(i);
        },
        hide: function () {
            $(this.defs.container).hide();
        }
    },
    //CONTENT MANGER
    contentManager: {
        switchView: function (url) {
            faqController.caetegoryList.show();
            if (url.match(/\/[0-9]+/)) {
                var i = url.replace(/[^0-9]/gi, '');
                if (url.match(/categoria\//)) { //preguntasByCategoria
                    //$('.faq-list')
                    faqController.topTen.hide();
                    faqController.preguntasByCategorias.show(i);
                    faqController.preguntasRelacionadas.hide();
                } else { //preguntaByID
                    //$('.como-funciona')
                    $('#video').hide();
                    faqController.preguntaById.show(i);

                    //$('.faq-list')
                    faqController.topTen.hide();
                    faqController.preguntasByCategorias.hide();
                    faqController.preguntasRelacionadas.show(i);
                }
            } else { //index
                //$('.como-funciona')
                $('#video').show();
                faqController.preguntaById.hide();
                
                //$('.faq-list')
                faqController.topTen.show(i);
                faqController.preguntasByCategorias.hide();
                faqController.preguntasRelacionadas.hide();
            }
        }
    },
    
    //---TOOLS--//

    //URL PARSER
    urlParser: {
        BASE_URL: scriptsController.defs.baseUrl+'faq/',
        encode: function(section, title) {
            if (typeof(section) == 'undefined')section = '';
            title = title || '';
            if (navigator.appName == "Microsoft Internet Explorer") {
                if (window.location.href.indexOf('#') == -1) {
                    document.location = this.BASE_URL+'#'+section;
                } else {
                    document.location.hash = '#'+section;
                }
            } else {
                window.history.replaceState('', title, this.BASE_URL+section);
            }
        },
        decode: function(callback){
            var section = document.location + '';
            section = section.replace(this.BASE_URL, '')
            .replace('#', '')
            .replace(/\/$/, '');
            callback(section);
        }
    },
    
    //COOKIE HELPER
    coockieHelper: {
        setCookie: function (nombre,valor,dias) {
            var vencimiento = new Date();
            vencimiento.setDate(vencimiento.getDate() + dias);
            valor = escape(valor) + ((dias==null) ? "" : "; expires="+vencimiento.toUTCString());
            document.cookie = nombre + "=" + valor;
        },
        getCookie: function (nombre) {
            var i,x,y,ARRcookies=document.cookie.split(";");
            for (i=0;i<ARRcookies.length;i++) {
                x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
                y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
                x=x.replace(/^\s+|\s+$/g,"");
                if (x==nombre) {
                    return unescape(y);
                }
            }
            return false;
        }
    },
    
    tallyCounter: {
        addVisitante: function (i) {
            if (!faqController.coockieHelper.getCookie('pregunta-'+i)) {
                $.ajax({
                    url: scriptsController.defs.baseUrl+'controllers/faqController/addVisitante/'+i
                })
                faqController.coockieHelper.setCookie('pregunta-'+i, 'true', 1);
            }
        }
    },
    
    //SCROLL TO COMPONENT
    scrollToComponent: {
	defs: {
	    load: false
	},
	load: function(){
	    var componentsPath = scriptsController.defs.baseUrl+scriptsController.defs.componentsPath+'scrollto/',
	    self = this;

	    $.getScript(componentsPath+'jquery.scrollTo-1.4.2-min.js', function(){
		self.defs.load = true;
	    });
	},
	init: function(){
	    if (!this.defs.load) this.load();
	}
    },
    
    //init
    init: function(){
        this.scrollToComponent.init();
        this.urlParser.decode(this.contentManager.switchView);
    }
}

$(function(){
    faqController.init();
});